package com.goods.business.service.imp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.mapper.OutStockInfoMapper;
import com.goods.business.mapper.OutStockMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.OutStockService;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.OutStock;
import com.goods.common.model.business.OutStockInfo;
import com.goods.common.model.business.Product;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.OutStockDetailVO;
import com.goods.common.vo.business.OutStockItemVO;
import com.goods.common.vo.business.OutStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author liangliang
 * @Date 2021/11/10 18:42 Administrator
 * @Description
 */
@Service
public class OutStockServiceImpl implements OutStockService {
    @Autowired
    private OutStockMapper outStockMapper;
    @Autowired
    private ConsumerMapper consumerMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OutStockInfoMapper outStockInfoMapper;
    /**
     * 物资发放记录
     * @param map
     * @return
     */
    @Override
    public PageVO<OutStockVO> findOutStockList(Map map) {
     List<OutStock>  outStockList=outStockMapper.findByOutStock(map);
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        List<OutStockVO> outStockVOList = outStockList.stream().map(outStock -> {
            OutStockVO outStockVO = new OutStockVO();
            Consumer consumer = consumerMapper.selectByPrimaryKey(outStock.getConsumerId());
            outStockVO.setAddress(consumer.getAddress());
            outStockVO.setPhone(consumer.getPhone());
            BeanUtils.copyProperties(outStock, outStockVO);
            return outStockVO;
        }).collect(Collectors.toList());
        //总记录数
        int total=outStockVOList.size();
        //最后一条记录(pageNum-1)*pageSize+pageSize; 从第几条开始显示 (pageNum-1)*pageSize
        int size=(pageNum-1)*pageSize+pageSize;
        List<OutStockVO> outStockVOList1=outStockVOList.subList((pageNum-1)*pageSize,size>total?total:size);
        PageVO<OutStockVO> pageVO = new PageVO<>(total,outStockVOList1);
        return pageVO;
    }

    /**
     * 查询物资的发放明细
     * @param id
     * @return
     */
    @Override
    public OutStockDetailVO detail(Long id) {
        OutStockDetailVO outStockDetailVO = new OutStockDetailVO();
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        //封装出库表

        BeanUtils.copyProperties(outStock,outStockDetailVO);

        //封装物资接收放的表
        Consumer consumer = consumerMapper.selectByPrimaryKey(outStock.getConsumerId());
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer,consumerVO);
        outStockDetailVO.setConsumerVO(consumerVO);
        //通过商品主表查询出库信息表

        //每一个出库单对应着多个出库主表信息biz_out_stock_info对象
        List<OutStockInfo> outStockInfoList=outStockMapper.oUtStockByPum(outStock.getOutNum());

        //遍历每一个出库主表信息对象，得到商品编号，从而得到商品编号对应的对象
        outStockInfoList.stream().forEach(outStockInfo -> {
            //查询每一个商品编号对应的商品
            List<Product> productList=productMapper.productByPum(outStockInfo.getPNum());
            //每一个商品编号都可能能有不同的商品
            List<OutStockItemVO> stockItemVOList = productList.stream().map(product -> {
                //product：是遍历出来的每一个商品
                OutStockItemVO outStockItemVO = new OutStockItemVO();
                outStockItemVO.setCount(productList.size());
                BeanUtils.copyProperties(product, outStockItemVO);
                return outStockItemVO;
            }).collect(Collectors.toList());
            outStockDetailVO.setItemVOS(stockItemVOList);
            outStockDetailVO.setTotal(stockItemVOList.size());
        });

        return outStockDetailVO;
    }

    /**
     * 将商品移入回收站
     * @param id
     */
    @Override
    public void remove(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        outStock.setStatus(1);
        outStockMapper.updateByPrimaryKey(outStock);
    }

    /**
     * 将回收站中的数据还原
     * @param id
     */
    @Override
    public void back(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        outStock.setStatus(0);
        outStockMapper.updateByPrimaryKey(outStock);
    }

    /**
     * 将回收站中的数据删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        outStockMapper.deleteByPrimaryKey(id);
    }

    /**
     * 审核通过
     * @param id
     */
    @Override
    public void publish(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        outStock.setStatus(0);
        outStockMapper.updateByPrimaryKey(outStock);
    }

    /**
     * 保存物资发放
     * @param outStockVO
     */
    @Override
    public void addOutStock(OutStockVO outStockVO) {
        //先判断有没有物资去向的id
        Consumer consumer = new Consumer();
        if (outStockVO.getConsumerId()==null){//如果没有新增去向
                //先保存新增去向表

            BeanUtils.copyProperties(outStockVO,consumer);
            consumer.setCreateTime(new Date());
            consumer.setModifiedTime(new Date());
            consumerMapper.insert(consumer);
        }
        //(如果是已知去向就不保存去向表，如果不是已知去向，就先保存新增去向，在保存剩下的两张表)
        String out_Num = UUID.randomUUID().toString();

        //保存出库表
        OutStock outStock = new OutStock();
        outStock.setOutNum(out_Num);
        outStock.setType(outStockVO.getType());
        outStock.setOperator(outStockVO.getOperator());
        outStock.setCreateTime(new Date());
        //outstock中的product_number=outStockinfo的总数相加
        //前端传过来的商品ID对应着商品数量
        List<Object> products = outStockVO.getProducts();
        int number=0;
        for (Object product : products) {
            String jsonString = JSONObject.toJSONString(product);
            Map map = JSON.parseObject(jsonString, Map.class);
            Integer productId = (Integer) map.get("productId");
            Integer productNumber = (Integer) map.get("productNumber");
            number+=productNumber;
            //根据商品ID查询商品信息
            Product product1 = productMapper.selectByPrimaryKey(productId);

            //保存出库的信息表
            OutStockInfo outStockInfo = new OutStockInfo();
            outStockInfo.setOutNum(out_Num);
            outStockInfo.setPNum(product1.getPNum());
            outStockInfo.setProductNumber(productNumber);
            outStockInfo.setCreateTime(new Date());
            outStockInfo.setModifiedTime(new Date());
            outStockInfoMapper.insert(outStockInfo);
        }
        outStock.setProductNumber(number);
        outStock.setConsumerId(consumer.getId());
        outStock.setRemark(outStockVO.getRemark());
        outStock.setStatus(2);
        outStock.setPriority(outStockVO.getPriority());
        outStockMapper.insert(outStock);
    }
}
